
const sequelize = require('../../lib/database.lib');
const Page = require('../../lib/page.lib');

/**
 * 抽象model
 */
class AbstractModel {
    constructor(tableName, attributes) {
        this.model = sequelize.define(tableName, attributes,
            { timestamps: false, freezeTableName: true }  //去除crateAt,updateAt;自定义表名
        );
        this.model.removeAttribute('id');
        this.sequelize = sequelize;
    }
    findAll(where = {}) {
        return this.model.findAll(where);
    }
    /**
     * 分页
     * @param {*} param
     */
    async paginationQuery({ condition = {} } = params) {
        let { pageNo, pageSize } = condition;
        let offset = pageNo * pageSize,
            limit = pageSize;
        Object.assign(condition, { offset, limit });
        let data = await this.model.findAndCountAll(condition);
        return new Page({ pageNo, pageSize, totalNumber: data.count, data: data.rows });
    }

}

module.exports = AbstractModel;
